#include <iostream>
#include <vector>

using namespace std;
class Solution
{

public:
    string gcdOfStrings(string str1, string str2)
    {
        int len1 = (int)str1.length(), len2 = (int)str2.length();
        auto check = [](string t, string s) -> bool
        {
            int lenx = (int)s.length() / (int)t.length();
            string ans = "";
            for (int i = 1; i <= lenx; ++i)
            {
                ans = ans + t;
            }
            return ans == s;
        };
        for (int i = min(len1, len2); i >= 1; --i)
        {
            if (len1 % i == 0 && len2 % i == 0)
            {
                string X = str1.substr(0, i);
                if (check(X, str1) && check(X, str2))
                    return X;
            }
        }
        return "";
    }

    string gcdOfStrings_2(string str1, string str2)
    {
        int len1 = (int)str1.length(), len2 = (int)str2.length();
        auto check = [](string t, string s) -> bool
        {
            int lenx = (int)s.length() / (int)t.length();
            string ans = "";
            for (int i = 1; i <= lenx; ++i)
            {
                ans = ans + t;
            }
            return ans == s;
        };
        string T = str1.substr(0, __gcd(len1, len2)); // __gcd() 为c++自带的求最大公约数的函数
        if (check(T, str1) && check(T, str2))
            return T;
        return "";
    }

    string gcdOfStrings(string str1, string str2)
    {
        if (str1 + str2 != str2 + str1)
            return "";
        return str1.substr(0, __gcd((int)str1.length(), (int)str2.length())); // __gcd() 为c++自带的求最大公约数的函数
    }
};
int main()
{

    system("pause");
    return 0;
}
